﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxAXVLC;
using AXVLC;

namespace MedusaVLC
{
    public partial class MedusaVLCPlayer : Form
    {
        Timer updateProgress = new Timer();
        List<AxAXVLC.AxVLCPlugin2> players = new List<AxAXVLC.AxVLCPlugin2>();
        Dictionary<AxAXVLC.AxVLCPlugin2, HScrollBar> scrolls = new Dictionary<AxAXVLC.AxVLCPlugin2, HScrollBar>();
        public MedusaVLCPlayer()
        {
            InitializeComponent();

            players.Add(axVLCPlugin_1);
            players.Add(axVLCPlugin_2);
            players.Add(axVLCPlugin_3);
            players.Add(axVLCPlugin_4);
            players.Add(axVLCPlugin_5);
            players.Add(axVLCPlugin_6);


            foreach (AxAXVLC.AxVLCPlugin2 player in players)
            {
                player.Volume = 0;
                player.audio.Volume = 0;

            } 
            scrolls[players[0]] = hScrollBar1;
            scrolls[players[1]] = hScrollBar2;
            scrolls[players[2]] = hScrollBar3;
            scrolls[players[3]] = hScrollBar4;
            scrolls[players[4]] = hScrollBar5;
            scrolls[players[5]] = hScrollBar6;
            updateProgress.Tick += new EventHandler(updateProgress_Tick);
            updateProgress.Interval = 250;
            updateProgress.Start();

        }

        void updateProgress_Tick(object sender, EventArgs e)
        {            
            try
            {
                foreach (AxAXVLC.AxVLCPlugin2 player in players)
                {
                    scrolls[player].Value = (int)(player.input.Position * 1000);

                    player.audio.Volume = player.Volume;
                }

            }
            catch { }


        }
        private void volumeScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            string whichscroll = ((HScrollBar)sender).Name.Substring(((HScrollBar)sender).Name.Length - 1);
            int whichPlayer = int.Parse(whichscroll);

                players[whichPlayer - 1].Volume = e.NewValue;

        }
        private void hScrollBar_Scroll(object sender, ScrollEventArgs e)
        {
            string whichscroll = ((HScrollBar)sender).Name.Substring(((HScrollBar)sender).Name.Length - 1);
            int whichPlayer = int.Parse(whichscroll);
            try
            {
                players[whichPlayer - 1].input.Position = ((double)e.NewValue) / 1000d;

            }
            catch { }
        }

        private void btn_openFile_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
            {
                e.Effect = DragDropEffects.All;
            }
        }

        private void btn_openFile_DragDrop(object sender, DragEventArgs e)
        {
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
            string whichButton = "";
            try
            {
                whichButton = ((Button)sender).Name.Substring(((Button)sender).Name.Length - 1);
            }
            catch {
                whichButton = ((Panel)sender).Name.Substring(((Panel)sender).Name.Length - 1); 
            }
            int whichPlayer = int.Parse(whichButton);
            try
            {
                players[whichPlayer - 1].playlist.add("file:///" + files[0]);
                players[whichPlayer - 1].playlist.next();
                players[whichPlayer - 1].playlist.play();
            }
            catch { }
        }
        private void btn_openFile_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Open";
            openFileDialog1.Filter = "All Files|*.*";
            string whichButton = ((Button)sender).Name.Substring(((Button)sender).Name.Length - 1);
            int whichPlayer = int.Parse(whichButton);
            try
            {
                if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    players[whichPlayer - 1].playlist.add("file:///" + openFileDialog1.FileName);
                    players[whichPlayer - 1].playlist.play();
                    players[whichPlayer - 1].playlist.next();
                }
            }
            catch { }
        }

        //Fast hide video on close so not visible while unloading
        private void MedusaVLCPlayer_FormClosing(object sender, FormClosingEventArgs e)
        {
            foreach (AxAXVLC.AxVLCPlugin2 player in players)
            {
                player.Visible = false;
            }
        }

        private void panel_Click(object sender, EventArgs e)
        {

            string whichButton = "";
            whichButton = ((Panel)sender).Name.Substring(((Panel)sender).Name.Length - 1); 
            int whichPlayer = int.Parse(whichButton);
            players[whichPlayer - 1].playlist.togglePause();
        }

        private void axVLCPlugin_1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyValue == 32)
            {
                ((AxAXVLC.AxVLCPlugin2)sender).playlist.togglePause();
            }
        }
        
    }
}
